<?php

namespace app\common\model;

use think\Model;
use app\common\validate\User as UserValidate;

class User extends Model
{
    //
    protected function setPasswdAttr($value)
    {
        return md5($value . md5(time()));
    }

    //自动完成注册时间
    protected $insert = ['regtime', 'salt'];

    protected function setSaltAttr()
    {
        return time();
    }

    protected function setRegtimeAttr()
    {
        return time();
    }

    public static function userRegister($data)
    {
        /**
         * 验证数据
         */
        $validate = new UserValidate();
        if(!$validate->check($data)){
            return ['code' => 0, "msg" => $validate->getError()];
        }
        $result = self::create($data, true);
        if ($result) {
            return ['code' => 1, 'msg' => '注册成功'];
        } else {
            return ['code' => 0, 'msg' => '注册失败'];
        }
    }
}
